package web.mb;


import java.io.Serializable;





import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import metier.entities.Client;
import metier.entities.Employe;
import metier.sess.IUtilisateurLocal;


@ManagedBean
@SessionScoped
public class loginnMB  implements Serializable{
	
	private String usernaem;
	private String pass;
	public boolean  isLong=false;
	
	////
	@EJB
	private IUtilisateurLocal metier;
	
	//client transporteur ou expéditeur
	private Client client=new Client();
	private Employe employe=new Employe();
	 
public Client  C;

public 	Employe e;
	private  boolean  remember;
	
	private boolean connect=false;
	
	
	public boolean admin=false;

	public boolean isConnect() {
		return connect;
	}
	public void setConnect(boolean connect) {
		this.connect = connect;
	}
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
	}
	public Employe getEmploye() {
		return employe;
	}
	public void setEmploye(Employe employe) {
		this.employe = employe;
	}
	public boolean isRemember() {
		return remember;
	}
	public void setRemember(boolean remember) {
		this.remember = remember;
	//
	
	
	}
	public String getUsernaem() {
		return usernaem;
	}
	public void setUsernaem(String usernaem) {
		this.usernaem = usernaem;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	
	public loginnMB() {
		super();
		// TODO Auto-generated constructor stub
	}
	//type 0 pour les clients et 1 pour les employes
	public String action(Long type)
	{
		///////////////
		
		if(type==0)
		{
			admin=false;
			
		if((client.getPassword().compareTo("")!=0) && (client.getUsername().compareTo("")!=0) && (client.getCID()!=null) )
        {
        	 
        	 Client ci=(Client)metier.getUtilisateurUserName(client,0);
       
        	 if(ci==null)
        	 {
        		 isLong=false;
        		 connect=false;
        	     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Invalid login , password or cid"));
        	     
 	           
        	 }
        	 else
        	 {
        		 isLong=true;
        		 connect=true;
        		C=ci;
        		 return "/frontOffice/welcome.xhtml";
        	 }
        	 
        
        }
        else
        {
        	 isLong=false;
    		 connect=false;
        	 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "all fields are required"));
        }
		
		}
		else
		{
			admin=true;
			if((employe.getPassword().compareTo("")!=0) && (employe.getUsername().compareTo("")!=0))
	        {
	        	 
	        	 Employe c=(Employe)metier.getUtilisateurUserName(employe,1);
	        	 if(c==null)
	        	 {
	        		 isLong=false;
	        		 connect=false;
	        		 
	        	     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Invalid login or password "));
	        	     
	 	           
	        	 }
	        	 else
	        	 {
	        	     e=c;
	        		 isLong=true;
	        		 connect=true;
	        		 
	        		 return "/Admin/Home.xhtml";
	        	 }
	        	 
	        }
	        else
	        {
	        	 isLong=false;
	    		 connect=false;
	        	 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "all fields are required"));
	        }
		}
	   //////////////
		
	
		 return "gh";
	}

}
